﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net;

namespace AgencyManagerSystem.Controllers
{
    public class BillController :  MyControllerBase
    {
        BillManager manager;
        BillFetcher fetcher;
        BillDetailFetcher dFetcher;
        public BillController() : base()
        {
            this.manager = new BillManager();
            this.fetcher = new BillFetcher();
            this.dFetcher = new BillDetailFetcher();
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Create)]
        public AsmMainDetail<BillModel, BillDetailModel> Set(BillModel main,string jsonBillDetailModel)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("SET:[RQS|{2}]{0}|[DETAIL]{1}", Util.ConvertToJson(main), jsonBillDetailModel, RequestIp));
            var details = Util.ConvertArrayFromJson<BillDetailModel>(jsonBillDetailModel);
            if (main == null)
            {
                DBHelper.Commit(false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                var errResponse = new AsmMainDetail<BillModel, BillDetailModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            foreach (var item in details)
            {
                item.Bill = main.AutoId;
            }
            var result = manager.Set(main, details);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( result.Result);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Delete)]
        public AsmObject<bool> Del(BillModel roleModel)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("DEL:[RQS|{1}]{0}", Util.ConvertToJson(roleModel), RequestIp));
            if (roleModel == null)
            {
                DBHelper.Commit(false);
                LogHelper.Err(this.GetType(), String.Format("DEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            var result = manager.Delete(roleModel);
            DBHelper.Commit(result.Result);
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
       
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Exec)]
        public AsmArray<BillModel> List(string jsonCondition, int pos, int size)
        {
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RQS|{1}]{0}", jsonCondition, RequestIp));
            int cnt = 0;
            var ls = fetcher.List(Util.ConvertArrayFromJson<Condition>(jsonCondition), pos, size, out cnt);
            DBHelper.Commit(true);
            var result = new AsmArray<BillModel>() { Data = ls, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Exec)]
        public AsmMainDetail<BillModel, BillDetailModel> Get(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("GET:[RQS|{1}]{0}", id, RequestIp));
            var main = fetcher.Get(id);
            if (main == null)
            {
                var errResponse = new AsmMainDetail<BillModel, BillDetailModel> { Data = main, Detail = null, Message = "找不到数据", Result = AmsCode.DataNotFound };
                DBHelper.Commit( false);
                LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            var detail = dFetcher.ListDetail(id);
            DBHelper.Commit(true);
            var result = new AsmMainDetail<BillModel, BillDetailModel> { Data = main, Detail = detail, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }

        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Exec)]
        public AsmArray<BillDetailModel> ListDetail(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTDETAIL:[RQS|{1}]{0}", id, RequestIp));
            var details = dFetcher.ListDetail(id);
            DBHelper.Commit(true);
            var result = new AsmArray<BillDetailModel>() { Data = details, Message = "操作成功", Result = AmsCode.Succ, Cnt = details.Count };
            LogHelper.Debug(this.GetType(), String.Format("LISTDETAIL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
    }
}